package com.aya.strategy;

import com.aya.mapper.model.ChildBean;
import com.aya.mapper.model.SimpleInjectBean;
import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.config.*;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.ManagedMap;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.*;
import org.springframework.context.annotation.Scope;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.util.StringValueResolver;

import javax.annotation.Resource;
import java.lang.annotation.Repeatable;
import java.util.HashMap;
import java.util.Map;
//@Scope("prototype")
//@Service
public class BeanDefinitionRegistryPostProcessorImpl implements BeanDefinitionRegistryPostProcessor , EnvironmentAware {

    Logger logger= Logger.getLogger(getClass());

    @Resource
    AppListener appListener;

    String name;

    Map<String,Object> mapTest;
    private Environment environment;

    /**
     * bw.setPropertyValues(new MutablePropertyValues(deepCopy));
     * @param registry
     * @throws BeansException
     */
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        logger.info("BeanDefinitionRegistryPostProcessorImpl.postProcessBeanDefinitionRegistry");
        RootBeanDefinition rootBeanDefinition = new RootBeanDefinition();
        rootBeanDefinition.setBeanClass(SimpleInjectBean.class);

        ManagedMap<TypedStringValue, BeanReference> stringObjectManagedMap = new ManagedMap<>();
        stringObjectManagedMap.put(new TypedStringValue("childBean"),new RuntimeBeanReference("childBean"));
        MutablePropertyValues propertyValues = new MutablePropertyValues();
        propertyValues.addPropertyValue("registryMap",stringObjectManagedMap);
        rootBeanDefinition.setPropertyValues(propertyValues);
        registry.registerBeanDefinition("registryMapTest", rootBeanDefinition);
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

        StringValueResolver valueResolver = new EmbeddedValueResolver(beanFactory);
        BeanDefinitionVisitor visitor = new BeanDefinitionVisitor(valueResolver);
        BeanDefinition beanDefinitionRegistryPostProcessorImpl = beanFactory.getBeanDefinition("beanDefinitionRegistryPostProcessorImplConfig");
        visitor.visitBeanDefinition(beanDefinitionRegistryPostProcessorImpl);
        logger.info("BeanDefinitionRegistryPostProcessorImpl.postProcessBeanFactory");
        Map<String,Object> map1 = new HashMap<>();
        map1.put("purchase",new Object());
        map1.put("redeem",new Object());
        Map<String,Object> map2 = new HashMap<>();
        map2.put("purchase",new Object());
        map2.put("redeem",new Object());
        map2.put("frozen",new Object());
        map2.put("unfrozen",new Object());
        beanFactory.registerSingleton("registryMap",map1);
        beanFactory.registerSingleton("frozenMap",map2);


    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Map<String, Object> getMapTest() {
        return mapTest;
    }

    public void setMapTest(Map<String, Object> mapTest) {
        this.mapTest = mapTest;
    }

    @Override
    public void setEnvironment(Environment environment) {

        this.environment = environment;
    }
}
